#include <cstdlib>
#include "ht_room.h"
Room::Room(void)
{
	
}
void Room::setRoomId(int _roomid)
{
	room_id = _roomid;	
}
int Room::getRoomId(void)
{
	return room_id;	
}
void Room::setRoomType(RoomType _roomtype)
{
	room_type = _roomtype;	
}
RoomType Room::getRoomType(void)
{
	return room_type;	
}
void Room::setRoomPrice(float _roomprice)
{
	room_price = _roomprice;	
}
float Room::getRoomPrice(void)
{
	return room_price;	
}
void Room::setRoomStatus(int _roomstatus)
{
	room_status = _roomstatus;	
}
int Room::getRoomStatus(void)
{
	return room_status;	
}
void Room::setRoomGuest(const string& _name,const string& _sex,const string& _id,int st)
{
		room_gst[st].setName(_name);
		room_gst[st].setSex(_sex);
		room_gst[st].setId(_id);
}
Guest* Room::getRoomGuest(void)
{
	return room_gst;	
}
Room* Room::find_id(Room* room,int room_id)
{
		for(int i=0;i<100;i++)
		{
			if(room[i].getRoomId() == room_id)
				return room+i;
		}
		return NULL;
}
int Room::check_in(Room* room)
{
		system("clear");
		if(room->getRoomStatus()>0)
		{
			cout << "此房间已经有人入住!" << endl;
			stdin -> _IO_read_ptr = stdin -> _IO_read_end;
			cout << "按任意键继续..." << endl;
			getch();
			return 0;
		}
		string _name;
		string _sex;
		string _id;
		int choice = 0;
		int id=-1;
		int cnt = 0;
		stdin -> _IO_read_ptr = stdin -> _IO_read_end;
		for(cnt=0;cnt<(int)room->getRoomType();cnt++)
		{
			cout << "按任意键添加成员..."<<endl;
			stdin -> _IO_read_ptr = stdin -> _IO_read_end;
			switch(getch())
			{
				case 27: return 0;break;
				case 10: id=0; break;
				default:  break;
			}
			if(id==0) break;
			cout << "请登记第"<<cnt+1<<"位旅客姓名/性别/身份证号:";
			cin >> _name >> _sex >> _id;
			room->setRoomGuest(_name,_sex,_id,cnt);
		}
		if(cnt == 0)
		{
			cout << "取消入住!" << endl;
			stdin -> _IO_read_ptr = stdin -> _IO_read_end;
			cout << "按任意键继续..." << endl;
			getch();
			return 0;	
		}
		id = room->getRoomId();
			cout << "入住成功!您的房号是:"<<id<<endl;
				room->setRoomStatus(cnt);	
		stdin -> _IO_read_ptr = stdin -> _IO_read_end;
		cout << "按任意键继续..." << endl;
		getch();
		return id;
}//入住
bool Room::check_out(Room* room)
{
	if(room->getRoomStatus()==0)
	{
		system("clear");
		cout << "该房间似乎没有人住..." << endl;
		stdin -> _IO_read_ptr = stdin -> _IO_read_end;
		cout << "按任意键继续..." << endl;
		getch();
		return false;
	}	
	for(int i=0;i<room->getRoomStatus();i++)
	{
		room->setRoomGuest("","","",i);	
	}
	room->setRoomStatus(0);
	system("clear");
	stdin -> _IO_read_ptr = stdin -> _IO_read_end;
	cout << "退房成功..." << endl;
	cout << "按任意键继续..." << endl;
	getch();
	return true;
}
void Room::show_room_msg(Room* room,int room_id,mode cmd)
{
	Room* roomx = find_id(room,room_id);
	system("clear");
	if(cmd == 0)
		cout << "房间号:" << roomx->getRoomId()<< " 房间类型:" << roomx->getRoomType() << " 房间价格:" << roomx->getRoomPrice() << " 房间人数:" << roomx->getRoomStatus() << endl;
	else
	{
		cout << "房间号:" << roomx->getRoomId()<< " 房间类型:" << roomx->getRoomType() << " 房间价格:" << roomx->getRoomPrice() << " 房间人数:" << roomx->getRoomStatus() << endl;
		for(int i=0;i<roomx->getRoomStatus();i++)
		{
			cout << "姓名:" << roomx->room_gst[i].getName() << " 性别:" <<roomx->room_gst[i].getSex() << " 身份证号:" << roomx->room_gst[i].getId() << endl; 	
		}
			
	}	
		stdin -> _IO_read_ptr = stdin -> _IO_read_end;
		cout << "按任意键继续..." << endl;
		getch();
}
void Room::help(void)
{
	system("clear");
	cout << "------指针大酒店帮助------" << endl;
	cout << "【方向键】左右控制翻页,上下控制当前选项" <<endl;
	cout << "【Enter键】选择指针指向的房间" << endl;
	cout << "【TAB键】察看当前房间信息" << endl;
	cout << "【DELETE键】选中房间退房" << endl;
	cout << "【HOME键】察看帮助" << endl;
	cout << "【ESC键】退出系统" << endl;
	cout << "---------------------" << endl;
	cout << "入住界面内"<<endl;
	cout << "【Esc】键:退出入住界面" << endl;
	cout << "【Enter】键:结算入住" << endl;
	stdin -> _IO_read_ptr = stdin -> _IO_read_end;
	cout << "按任意键继续..." << endl;
	getch();

}
int Room::displayAllRoom(Room* _room,RoomType type)
{
	Room* roomx = new Room[100];
	while(1)
	{
		int cnt = 0;
		for(int i=0;i<100;i++)
		{
			if(type == _room[i].getRoomType()||type == All)
			{
				roomx[cnt].setRoomId(_room[i].getRoomId());
				roomx[cnt].setRoomType(_room[i].getRoomType());
				roomx[cnt].setRoomPrice(_room[i].getRoomPrice());
				roomx[cnt++].setRoomStatus(_room[i].getRoomStatus());
				
			}	
		}
		int page = cnt/10;
		int rent = cnt%10;
		int curpage = 0;
		int flag=0;
		int cur=0;
	while(!flag){
		stdin -> _IO_read_ptr = stdin -> _IO_read_end;
		system("clear");
	for(int i = curpage*10;i<(page==curpage?page*10+rent:curpage*10+10);i++){
		if(cur!=i){
		cout << "   " <<"房间号:" << roomx[i].getRoomId()<< " 房间类型:" << roomx[i].getRoomType() << " 房间价格:" << roomx[i].getRoomPrice() << " 房间人数:" << roomx[i].getRoomStatus() << endl;
			}
		else
		{
		cout << "->" << "【房间号:" << roomx[i].getRoomId()<< " 房间类型:" << roomx[i].getRoomType() << " 房间价格:" << roomx[i].getRoomPrice() << " 房间人数:" << roomx[i].getRoomStatus() << "】" << endl;
			}

		}
		cout << "                   " << "第【"<<curpage+1<<"/"<<page+1<<"】页"<<endl;
		stdin -> _IO_read_ptr = stdin -> _IO_read_end;
		switch(getch())
		{
			case 186:if(curpage>0){curpage--;cur-=10;}break;
			case 185:if(curpage<page){ curpage++; cur+=10;}if(cur>=cnt) cur=cnt;break;
			case 183:if(cur>curpage*10) cur--; break;
			case 184:if(cur<(page==curpage?page*10+rent:curpage*10+10)-1) cur++; break;
			case 10:check_in(find_id(_room,roomx[cur].getRoomId()));flag=1; break;
			case 9:show_room_msg(_room,roomx[cur].getRoomId(),1); flag=1;break;
			case 295:check_out(find_id(_room,roomx[cur].getRoomId()));flag=1;break;
			case 27:delete[] roomx; return 0;break;
			case 178:help(); break;
			default:break;
		}

		}
	}
	return 0;
}
int Room::find_empty_room(Room* _room,RoomType type)
{
	for(int i=0;i<100;i++)
	{
		if(type == _room[i].getRoomType() && _room[i].getRoomStatus()==0)
		{
			return _room[i].getRoomId();	
		}	
	}
	return 0;
}
